//package com.homedo.microservice.open.service.gateway.zuul.fallback;
//
//import com.alibaba.fastjson.JSONObject;
//import com.homedo.common.bean.data.ResponseData;
//import com.homedo.common.bean.data.enums.ResponseEnum;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.client.ClientHttpResponse;
//import org.springframework.stereotype.Component;
//
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.nio.charset.Charset;
//
//@Component
//public class LocalFallbackProvider implements FallbackProvider {
//    private static final Logger _LOGGER = LoggerFactory.getLogger(LocalFallbackProvider.class);
//
//    @Override
//    public ClientHttpResponse fallbackResponse(Throwable cause) {
//        _LOGGER.warn("[调用出现错误:{}]", cause.getMessage(), cause);
//        return this.fallbackResponse(cause.getMessage());
//    }
//
//    @Override
//    public String getRoute() {
//        String serverApplicationName = "*";//拿到目标服务名
//        return serverApplicationName;
//    }
//
//    @Override
//    public ClientHttpResponse fallbackResponse() {
//        String fallbackDesc = null;
//        return this.fallbackResponse(fallbackDesc);
//    }
//
//    private ClientHttpResponse fallbackResponse(String fallbackDesc) {
//        return new ClientHttpResponse() {
//            private ByteArrayInputStream responseByteArrayInputStream = null;
//
//            @Override
//            public HttpStatus getStatusCode() throws IOException {
//                return HttpStatus.OK;
//            }
//
//            @Override
//            public int getRawStatusCode() throws IOException {
//                return this.getStatusCode().value();
//            }
//
//            @Override
//            public String getStatusText() throws IOException {
//                return this.getStatusCode().getReasonPhrase();
//            }
//
//            @Override
//            public void close() {
//                if (responseByteArrayInputStream != null) {
//                    try {
//                        responseByteArrayInputStream.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//
//            @Override
//            public InputStream getBody() throws IOException {
//                ResponseData responseData = new ResponseData();
//                responseData.setResponseEnum(ResponseEnum.UNKNOWN);
//                if (fallbackDesc != null) {
//                    responseData.setRespDesc(fallbackDesc);
//                } else {
//                    responseData.setRespDesc("default fallback");
//                }
//                responseByteArrayInputStream = new ByteArrayInputStream(JSONObject.toJSONString(responseData).getBytes());
//                return responseByteArrayInputStream;
//            }
//
//            @Override
//            public HttpHeaders getHeaders() {
//                HttpHeaders headers = new HttpHeaders();
//                MediaType mt = new MediaType("application", "json", Charset.forName("UTF-8"));
//                headers.setContentType(mt);
//                return headers;
//            }
//        };
//    }
//}
